<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>编辑功能页面</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <script src="../../res/js/jquery-3.1.1.min.js" type="text/javascript" charset="utf-8"></script>
    <link rel="stylesheet" href="../../res/layuis/dist/css/layui.css"  media="all">
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
    <script src="../../res/layuis/dist/layui.js" charset="utf-8"></script>
    <!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->
    <script src="../../res/js/login_common.js" charset="UTF-8"></script>
    <style>
        body{margin: 10px;}
        .demo-carousel{height: 200px; line-height: 200px; text-align: center;}
    </style>
    <script type="text/javascript">
        //全局同步
        $.ajaxSetup({
            async: false
        });
        function getQueryString(name){
            var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
            var r = window.location.search.substr(1).match(reg);
            if(r!=null)return  unescape(r[2]);
            return null;
        }
        var attachTypeId = getQueryString("attachTypeId");
        console.log("辅助功能类型:"+attachTypeId);
    </script>
</head>
<body>

<form class="layui-form layui-header" action="" lay-filter="example">
    <div class="layui-form-item">
        <div class="layui-input-inline">
            <select id="auditStatus">
                <option value="0">审核状态</option>
                <option value="10">已发布</option>
                <option value="20">待审核</option>
                <option value="25">未通过</option>
            </select>
        </div>
        <div class="layui-input-inline">
            <select id="searchCondition" lay-filter="condition">
                <option value="0">请选择查询条件</option>
                <option value="1" >标题</option>
                <option value="2">备注</option>
                <option value="4">发布人</option>
            </select>
        </div>
        <div class="layui-input-inline">
            <input type="text" id="keyword" placeholder="关键字" autocomplete="off" class="layui-input">
        </div>
        <div class="layui-input-inline">
            <input type="text" id="beginTime" class="layui-input test-item" placeholder="起始时间">
        </div>
        <div class="layui-input-inline">
            <input type="text" id="endTime" class="layui-input test-item" placeholder="结尾时间">
        </div>
        <div class="layui-input-inline">
            <a class="layui-btn layui-bg-blue  " lay-event="search" id="search">查询</a>
        </div>
    </div>
</form>
<table class="layui-hide" id="demo" lay-filter="test"></table>

<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">浏览</a>
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-xs" lay-event="opinion">意见</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<script type="text/html" id="toolbarDemo">
    <div class="layui-form-item">
        <div class="layui-inline">
            <a class="layui-btn layui-bg-blue  " lay-event="delete">批量删除</a>
            <a class="layui-btn layui-bg-blue  " lay-event="add">新增</a>
        </div>
    </div>
</script>

<script type="text/html" id="tempLetState">
    {{#  if(d.auditState == 10){ }}
    <span>通过</span>
    {{#  } else if(d.auditState == 20){ }}
    <span>等待审核</span>
    {{#  } else if(d.auditState == 25){ }}
    <span>未通过</span>
    {{#  } }}
</script>


<script>

    layui.config({
        version: '1572350921010' //为了更新 js 缓存，可忽略
    });

    layui.use([ 'layer', 'table', 'element', 'laydate','form'], function(){
        var layer = layui.layer //弹层
            ,table = layui.table //表格
            ,element = layui.element //元素操作
            ,form = layui.form
            ,laydate = layui.laydate; //表单



        laydate.render({
            elem: '#beginTime'
            ,isInitValue: true
        });
        laydate.render({
            elem: '#endTime'
            ,isInitValue: true
        });

        //监听Tab切换
        element.on('tab(demo)', function(data){
            layer.tips('切换了 '+ data.index +'：'+ this.innerHTML, this, {
                tips: 1
            });
        });
        var userId = window.loginObj.manager.usId;
        var userName = window.loginObj.manager.usName;
        var url = "../../attachment/selectByUserId";
        //执行一个 table 实例
        table.render({
            elem: '#demo'
            //,height: 570
            ,even: true //隔行背景
            ,url: url //数据接口
            ,title: '用户表'
            ,page: true //开启分页
            ,toolbar: '#toolbarDemo' //开启工具栏，此处显示默认图标，可以自定义模板，详见文档
            ,where:{
                attachType:attachTypeId
                ,userId: userId
                ,userName:userName
            }
            ,cols: [[ //表头
                {type: 'checkbox', fixed: 'left'}
                ,{field: 'attachId', title: 'ID', width:60, sort: true, fixed: 'left',hide:true}
                ,{field: 'attachName', title: '标题', width:120}
                ,{field: 'attachContent', title: '内容'}
                ,{field: 'attachTime', title: '发布时间', width:120, sort: true}
                ,{field: 'attachValidTime', title: '有效时间', width: 120, sort: true}
                ,{field: 'attachPublisher', title: '发布人', width:120}
                //,{field: 'file1', title: '附件1', width: 100, templet: '#tempLetFile1'}
                //,{field: 'file2', title: '附件2', width: 100, templet: '#tempLetFile2'}
                //,{field: 'file3', title: '附件3', width: 100, templet: '#tempLetFile3'}
                ,{field: 'auditState', title: '审核状态', width: 100, templet: '#tempLetState'}
                //,{field: 'creatTime', title: '创建时间', width: 120, sort: true}
                ,{field: 'remark', title: '备注', width: 120}
                ,{fixed: 'right', width: 220, align:'center', toolbar: '#barDemo'}
            ]]
        });

        //下拉列表框点击事件
        form.on('select(condition)', function (data) {
            var searchCondition = $("#searchCondition").val();
            console.log("================"+searchCondition);
        });

        //搜索框点击事件
        $("#search").on("click",function () {
            var searchCondition = $("#searchCondition").val();
            var keyword = $("#keyword").val();
            var auditStatus = $("#auditStatus").val();
            var beginTime = $("#beginTime").val();
            var endTime = $("#endTime").val();
            table.reload('demo',{
                url: url //数据接口
                ,where:{
                    searchCondition:searchCondition,
                    keyword:keyword,
                    auditStatus:auditStatus,
                    beginTime:beginTime,
                    endTime:endTime
                }
                ,page:{
                    page : 1
                }
            });
            //layer.msg('查询：'+searchCondition+keyword+auditStatus);
        });

        //监听头工具栏事件
        table.on('toolbar(test)', function(obj){
            var checkStatus = table.checkStatus('demo');
            var data = checkStatus.data; //获取选中的数据
            var deleteAttachIds = ''; //删除id数组
            switch(obj.event){
                case 'add':
                    //layer.msg('新增数据');
                    layer.open({
                        type : 2,
                        title : '功能新增页面',
                        shadeClose : true,
                        shade : 0.8,
                        area : [ '80%', '80%' ],
                        content : 'AttachmentAdd.html', //iframe的url
                        success: function(layero, index){
                            var body = layer.getChildFrame('body', index);
                            var iframeWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法：
                            //console.log('============'+iframeWin);
                            body.find("#attachType").val(attachTypeId);
                            //iframeWin.begins();
                        },
                        end : function() {
                            //table.reload('demo',{});
                        }
                    });
                    break;
                case 'delete':
                    if(data.length === 0){
                        layer.msg('请选择一行');
                    } else {
                        layer.confirm('确定删除选中的'+data.length+'条数据吗？', function(index){
                            layer.close(index);
                            layui.each(data,function(index,item){
                                deleteAttachIds += item.attachId + ',';
                            });
                            //var deleteAttachIds = layui.$.map(data, function(d){return d.attachId;});
                            console.log('删除 [id]：'+deleteAttachIds);
                            //向服务端发送删除指令
                            $.ajax({
                                type: 'post',
                                url: '../../attachment/attachDelete', //数据接口
                                data: {
                                    'deleteAttachIds':deleteAttachIds
                                },
                                dataType: 'json',
                                success: function (data) {
                                    if(data.state > 0){
                                        layer.msg('删除成功',{
                                            icon: 1,
                                            time: 1000,
                                            end: function () {
                                                table.reload('demo',{});
                                                //window.location.reload();
                                            }
                                        });
                                    }else{
                                        layer.msg('删除失败',{
                                            icon: 1,
                                            time: 1000,
                                            end: function () {
                                                table.reload('demo',{});
                                                window.location.reload();
                                            }
                                        });
                                    }

                                }
                            });

                        });


                    }
                    break;
            }
        });

        //监听行工具事件
        table.on('tool(test)', function(obj){ //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            var data = obj.data //获得当前行数据
                ,layEvent = obj.event; //获得 lay-event 对应的值
            var deleteAttachIds = ''; //数组id
            if(layEvent === 'detail'){
                layer.open({
                    type: 2,
                    title : '通知页面',
                    shadeClose : true,
                    shade : 0.8,
                    area : [ '100%', '100%' ],
                    content: 'AttachmentShow.html',
                    success: function(layero, index){
                        var body = layer.getChildFrame('body', index);
                        var iframeWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法：
                        //console.log('============'+iframeWin);
                        body.find("#attachId").val(data.attachId);
                        body.find("#attachType").val(data.attachType);
                        iframeWin.beginShow();
                    },
                    end : function() {
                        //table.reload('demo',{});
                    }
                });
            } else if(layEvent === 'edit'){
                layer.open({
                    type: 2,
                    title : '功能修改页面',
                    shadeClose : true,
                    shade : 0.8,
                    area : [ '80%', '80%' ],
                    content: 'AttachmentUpdate.html',
                    success: function(layero, index){
                        var body = layer.getChildFrame('body', index);
                        var iframeWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法：
                        //console.log('============'+iframeWin);
                        body.find("#attachId").val(data.attachId);
                        iframeWin.begins();
                    },
                    end : function() {

                    }
                });

            } else if(layEvent === 'opinion'){
                //审核意见
                layer.open({
                    type: 2,
                    title : '审核意见',
                    shadeClose : true,
                    shade : 0.8,
                    area : [ '80%', '80%' ],
                    content: 'Opinion.html',
                    success: function(layero, index){
                        var body = layer.getChildFrame('body', index);
                        var iframeWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法：
                        //console.log('============'+iframeWin);
                        body.find("#attachId").val(data.attachId);
                        iframeWin.begins();
                    },
                    end : function() {

                    }
                });

            } else if(layEvent === 'del'){
                layer.confirm('确定删除选中行数据吗？', function(index){
                    //obj.del(); //删除对应行（tr）的DOM结构
                    layer.close(index);
                    deleteAttachIds += data.attachId;
                    console.log('删除数据id：'+deleteAttachIds);
                    layer.msg('删除成功', { icon: 1 });
                    //向服务端发送删除指令
                    $.ajax({
                        type: 'post',
                        url: '../../attachment/attachDelete', //数据接口
                        data: {
                            'deleteAttachIds':deleteAttachIds
                        },
                        dataType: 'json',
                        success: function (data) {
                            if(data.state > 0){
                                layer.msg('删除成功',{
                                    icon: 1,
                                    time: 1000,
                                    end: function () {
                                        table.reload('demo',{});
                                        //window.location.reload();
                                    }
                                });
                            }else{
                                layer.msg('删除失败',{
                                    icon: 1,
                                    time: 1000,
                                    end: function () {
                                        table.reload('demo',{});
                                        //window.location.reload();
                                    }
                                });
                            }

                        }
                    });
                });

            }
        });

    });


</script>
</body>
</html>